var dataUrl = null;
$(function () {
    $("form").validate({
        submitHandler: function (form) {// 必须写在验证前面，否则无法ajax提交
            //jquery获取复选框值
            if (!checkForm()) {
                return;
            }
            var businessArea = getBusinessArea();
            $("#businessArea").val(businessArea);
            // var chk_value =[];//定义一个数组
            var index = layer.load(1, {shade: false});
            $("#submitButton").attr("disabled", "disabled");
            $(form).ajaxSubmit({
                dataType: "text",
                success: function (data) {
                    $("#submitButton").removeAttr("disabled");
                    layer.close(index);
                    if (data == "success") {
                        // layer.confirm('添加成功!是否关闭窗口?', function (index) {
                        // layer.msg("添加成功！");
                        // window.location.reload();
                        window.location.href=getRootPath()+'/modules/winSuccess.html';
                        return false;
                        // });
                    } else {
                        layer.alert('添加失败！', 3);
                    }
                }
            });
        },
        errorPlacement: function (error, element) {// 自定义提示错误位置
            $(".l_err").css('display', 'block');
            // element.css('border','3px solid #FFCCCC');
            $(".l_err").html(error.html());
        },
        success: function (label) {// 验证通过后
            $(".l_err").css('display', 'none');
        }
    });
});
//js获取项目根路径，如： http://localhost:8083/uimcardprj
function getRootPath(){
    //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
    var curWwwPath=window.document.location.href;
    //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
    var pathName=window.document.location.pathname;
    var pos=curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8083
    var localhostPaht=curWwwPath.substring(0,pos);
    //获取带"/"的项目名，如：/uimcardprj
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
    return(localhostPaht+projectName);
}
function checkForm() {
    if ($("#fullName").val() == '') {
        layer.msg('姓名不能空');
        return false;
    }
    if ($("#phone").val() == '') {
        layer.msg('电话不能为空');
        return false;
    }
    if (!isPoneAvailable($("#phone").val())) {
        layer.msg('手机号码不正确');
        return false;
    }
    if ($("#corporateName").val() == '') {
        layer.msg('公司不能为空');
        return false;
    }
    return true;
}

function getBusinessArea() {  //jquery获取复选框值
    var s = '';
    $('input[name="area"]:checked').each(function () {
        if ($(this).val() == 1) {
            s += "品牌,"
        }
        if ($(this).val() == 2) {
            s += "买手集成店,"
        }
        if ($(this).val() == 3) {
            s += "批发,"
        }
        if ($(this).val() == 4) {
            s += "零售终端,"
        }
        if ($(this).val() == 5) {
            s += "其他,"
        }
    });
    if ($("#otherText").val() != '') {
        s += $("#otherText").val()
    }
    return s;
}
function isPoneAvailable(pone) {
    var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
    if (!myreg.test(pone)) {
        return false;
    } else {
        return true;
    }
}